#include "PickNum.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

PickNum::PickNum(QWidget *parent) : QWidget(parent)
{
    setFixedSize(50 * 3, 50 * 3 + 30);
    setWindowFlag(Qt::FramelessWindowHint);
    m_close = new QPushButton("取消", this);
    m_close->setGeometry(50, 50 * 3 + 10, 50, 20);
    m_close->show();
    connect(m_close, SIGNAL(clicked()), this, SLOT(hide()));
    m_select = 0;
}

void PickNum::setNum(int num)
{
    m_select = num;
    update();
}

void PickNum::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(QPen(Qt::lightGray));
    painter.setBrush(QBrush(QColor(Qt::green)));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if(m_select == (i * 3 + j + 1))
            {
                painter.save();
                painter.setPen(QPen(Qt::lightGray));
                painter.setBrush(QBrush(QColor(Qt::red)));
                painter.drawRect(j * 50, i * 50, 50, 50);
                painter.restore();
            }
            else
            {
                painter.drawRect(j * 50, i * 50, 50, 50);
            }
        }
    }

    painter.setPen(QPen(Qt::black, 3));
    painter.setFont(QFont("黑体", 30));
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            painter.drawText(50 * j + 18, 50 * i + 38, QString::number(i * 3 + j + 1));
        }
    }
}

void PickNum::mousePressEvent(QMouseEvent *e)
{
    int i = e->y() / 50;
    int j = e->x() / 50;
    int num = i * 3 + j + 1;
    if(num > 9)
    {
        return;
    }
    emit sig_clickNum(num);

    //qDebug() <<"pickNum:" << i << " " << j << " " << num;
    hide();
}
